package com.aep.provisioning.annotations.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.aep.provisioning.annotations.controller.validator.ContactValidator;
import com.aep.provisioning.controller.form.ContactForm;
import com.aep.provisioning.domain.Reason;
import com.aep.provisioning.service.WebFacade;
import com.google.gson.Gson;

@Controller
@SessionAttributes("contactForm")
public class GsonController {

	private transient static Logger logger = Logger.getLogger(JsonController.class);
	@Autowired
	private WebFacade webFacade;
	@Autowired
	private ContactValidator contactValidator;
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.setValidator(this.contactValidator);
	}
	
	
	@RequestMapping(value = "/internal/newGson",method=RequestMethod.GET)
	public @ModelAttribute("contactForm") ContactForm viewForm(){
		logger.info(" =========== VIEWFORM ========== " + webFacade);
		ContactForm contactForm = new ContactForm();
		contactForm.setDescription("Here verify");
		contactForm.setReasonList(getReasonList());
		logger.info(" =size reason == " + contactForm.getReasonList().size());
		contactForm.setDepartamentoList(webFacade.listDepartamentos());
		
		return contactForm ;
	}
	
	
	
	@RequestMapping(value = "/internal/newGson",method=RequestMethod.POST,params = "action=save")
	public 	@ModelAttribute("contactForm") ModelAndView saveBillingType(@ModelAttribute("contactForm") @Valid ContactForm contactForm,BindingResult result, SessionStatus status){
		ModelAndView mav = new ModelAndView("contactForm");
		if ( result.hasErrors() ) {
			mav.setViewName("internal/newJson");
			return mav;
		}else{
			logger.info(" =========== save ==========");
			StringBuilder addString = new StringBuilder("");
			for (String description : contactForm.getReasonDescriptions()) {
				if(("").equals(addString.toString().trim())){
					addString.append(description);
				}else{
					addString.append(" ,").append(description);
				}
			}
			contactForm.setInterest(addString.toString());
			webFacade.saveContact(contactForm);
		}
		status.setComplete();
		mav.setViewName(getUrl());
		return mav;
	}
	
	@RequestMapping(value = "/internal/newGson",method=RequestMethod.POST,params = "action=combox", produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody String fillCombox(@ModelAttribute("contactForm") ContactForm contactForm){

		logger.info(" GSON ");
		logger.info("contactForm.getIdDepartamento() : " + contactForm.getDepartamento());
		if(contactForm != null && contactForm.getDepartamento() != null && !("").equals(contactForm.getDepartamento())){
			contactForm.setProvinciaList(webFacade.listProvincias(Long.parseLong(contactForm.getDepartamento())));
			logger.info("size :: " + contactForm.getProvinciaList().size());
			
			String jsonOutput = new Gson().toJson(contactForm);
			logger.info(" json  : " + jsonOutput);
				
		}
		return new Gson().toJson(contactForm);
	}
	
	
    private String getUrl(){
    	return "redirect:index.html";
    }
	private List<Reason> getReasonList(){
		List<Reason> reasonList = new ArrayList<Reason>();
		for (int i = 1; i < 20; i++) {
			Reason reason = new Reason();
			reason.setId((long) i);
			reason.setDescription("Interest -  " + i);
			reasonList.add(reason);
		}
		return reasonList;
	}
	
}
